//{
/**
 * @fileOverview This file contains Husky plugin that takes care of the operations related to detecting the style change
 * @name hp_SE_WYSIWYGStyleGetter.js
 */
StyleGetter = jindo.$Class({
	
	oStyleMap : {
		fontFamily : {
			type : "Value",
			css : "fontFamily"
		},
		fontSize : {
			type : "Value",
			css : "fontSize"
		},
		fontColor : {
			type : "Value",
			css : "color"
		},
		lineHeight : {
			type : "Value",
			css : "lineHeight",
			converter : function(sValue, oStyle){
				if(!sValue.match(/px$/)){
					return sValue;
				}
				return Math.ceil((parseInt(sValue, 10)/parseInt(oStyle.fontSize, 10))*10)/10;
			}
		},
		bold : {
			command : "bold"
		},
		underline : {
			command : "underline"
		},
		italic : {
			command : "italic"
		},
		lineThrough : {
			command : "strikethrough"
		},
		superscript : {
			command : "superscript"
		},
		subscript : {
			command : "subscript"
		},
		justifyleft : {
			command : "justifyleft"
		},
		justifycenter : {
			command : "justifycenter"
		},
		justifyright : {
			command : "justifyright"
		},
		justifyfull : {
			command : "justifyfull"
		},
		orderedlist : {
			command : "insertorderedlist"
		},
		unorderedlist : {
			command : "insertunorderedlist"
		},
		letterSpacing : {
			type : "Value",
			css : "letterSpacing"
		},
		textTransform : {
			type : "Value",
			css : "textTransform"
		},
		hyperLink : {
			type : "Value",
			attr : "href"
		},
		isHyperLink : {			
		},
		backgroundColor : {
			type : "Value",
			css : "backgroundColor"
		}
	},

	$init : function(){
		this.oStyle = this._getBlankStyle();
	},
			
	getCurrentStyle : function(){
		return this.oStyle;
	},
		
	_getBlankStyle : function(){
		var oBlankStyle = {};
		for(var attributeName in this.oStyleMap){
			if(this.oStyleMap[attributeName].type == "Value"){
				oBlankStyle[attributeName] = "";
			}else{
				oBlankStyle[attributeName] = 0;
			}
		}
		
		return oBlankStyle;
	},

	_getStyleOf : function(oNode, oDoc){


		var oStyle = this._getBlankStyle();
		
		// this must not happen
		if(!oNode){
			return oStyle;
		}
		
		if( oNode.nodeType == 3 ){
			oNode = oNode.parentNode;
		}else if( oNode.nodeType == 9 ){
			//document에는 css를 적용할 수 없음.
			oNode = oNode.body;
		}
		
		var welNode = jindo.$Element(oNode);
		var attribute, cssName;

		for(var styleName in this.oStyle){
			
			attribute = this.oStyleMap[styleName];
			if(attribute.type && attribute.type == "Value"){
				try{
					if(attribute.css){
						var sValue = welNode.css(attribute.css);
						if(styleName == "fontFamily"){
							sValue = sValue.split(/,/)[0];
						}
		
						oStyle[styleName] = sValue;
						
					} else if(attribute.command){
						oStyle[styleName] = this.oDocument.queryCommandState(attribute.command);
					} else if(attribute.attr && attribute.attr == "href") { //HyperLink
						var sValue = welNode.attr(attribute.attr);
						if ( sValue == null ) { 
							sValue = "";
							oStyle["isHyperLink"] = false;
						} else {
							oStyle["isHyperLink"] = true;
						}
						oStyle[styleName] = sValue;
					} else {
						// todo
					}
				}catch(e){}
			}else{
				if(attribute.command){
					try{
						if(this.oDocument.queryCommandState(attribute.command)){
							oStyle[styleName] = "@^";
						}else{
							oStyle[styleName] = "@-";
						}
					}catch(e){
						console.log(e);						
					}
				}else{
					// todo
				}
			}
		}
		switch(oStyle["textAlign"]){
		case "left":
			oStyle["justifyleft"]="@^";
			break;
		case "center":
			oStyle["justifycenter"]="@^";
			break;
		case "right":
			oStyle["justifyright"]="@^";
			break;
		case "justify":
			oStyle["justifyfull"]="@^";
			break;
		}
		
		// IE에서는 기본 정렬이 queryCommandState로 넘어오지 않아서 정렬이 없다면, 왼쪽 정렬로 가정함
		if(oStyle["justifyleft"]=="@-" && oStyle["justifycenter"]=="@-" && oStyle["justifyright"]=="@-" && oStyle["justifyfull"]=="@-"){oStyle["justifyleft"]="@^";}
		
		return oStyle;
	},
	_getStyle : function(oFirstChild, oDocument, oWindow){
		this.oDocument = oDocument;
		var oStyle;
				
		var oSelection = new nhn.HuskyRange(oWindow);			
		oSelection.selectNode(oFirstChild);	
		oSelection.select();
		
		var funcFilter = function(oNode){
			if (!oNode.childNodes || oNode.childNodes.length == 0)
				return true;
			else
				return false;
		}

		//var aBottomNodes = oSelection.getNodes(false, funcFilter);
		var aBottomNodes = oSelection.getNodes(true, funcFilter);
		
		if(aBottomNodes.length == 0){			
			oStyle = this._getStyleOf(oFirstChild);//oSelection.commonAncestorContainer);
		}else{
			oStyle = this._getStyleOf(aBottomNodes[0]);
		}
		
		
		for(attributeName in oStyle){
			if(this.oStyleMap[attributeName].converter){
				oStyle[attributeName] = this.oStyleMap[attributeName].converter(oStyle[attributeName], oStyle);
			}
			if(this.oStyle[attributeName] != oStyle[attributeName]){
				//this.oApp.exec("MSG_STYLE_CHANGED", [attributeName, oStyle[attributeName]]);
			}
		}
		
		this.oStyle = oStyle;
	}
});

function getContentStyle(strObjID) {		
	var oStyleGetter = new StyleGetter();
	var objDiv = $(strObjID);
	if (objDiv) {			
		$(strObjID).contentEditable = true;			
		oStyleGetter._getStyle(objDiv, document, window);		
		$(strObjID).contentEditable = false;		
	} 	
	var style = oStyleGetter.getCurrentStyle();
	return style;
}
//}